'''
Created on 2020年1月30日
获取所有股票数据
@author: JM
'''
import pandas as pd

from app_config import get_engine, get_pro
from sqlalchemy import text

engine = get_engine()


def read_data():
    sql = """SELECT distinct * FROM stock_basic"""
    data_frame = pd.read_sql_query(sql, engine)
    return data_frame


def write_data(data_frame):
    res = data_frame.to_sql('stock_basic', engine, index=False, if_exists='append', chunksize=5000)
    print(res)


def rewrite_data(data_frame):
    sql = """TRUNCATE TABLE stock_basic"""
    with engine.connect() as conn:
        conn.execute(text(sql))
    res = data_frame.to_sql('stock_basic', engine, index=False, if_exists='append', chunksize=5000)
    print(res)


def get_data():
    data = get_pro().stock_basic(exchange='', list_status='L',
                                 fields='ts_code,symbol,name,exchange,market,area,industry,list_date')
    return data


if __name__ == '__main__':
    df = get_data()
    rewrite_data(df)
    print(df)
